<template>
  <el-card v-for="(item, index) in showdata" :key="index" class="box-card">
    <template #header>
      <div class="card-header">
        <div v-html="item.title"></div>
      </div>
    </template>

    <div v-for="(items, indexs) in item.questionItemObjects" :key="indexs">
      <br />
      {{ items.prefix }}: {{ items.content }}
    </div>
    <div>请输入你的答案：</div>
    <br />
    <el-input
      v-if="!item.questionItemObjects"
      v-model="item.endscore"
      placeholder="Please input"
      style="width: 30%"
      :rows="5"
      type="textarea"
    />
    <el-input
      v-else
      v-model="item.endscore"
      placeholder="Please input"
      style="width: 30%"
    />
  </el-card>
  <br />
  <div style="width: 100%">
    <el-button type="primary" style="width: 100%" @click="submit"
      >交卷</el-button
    >
  </div>
</template>

<script setup>
import { mydata } from '@/api/user'
import router from '@/router'
import store from '@/store'
import { ref } from 'vue'
import {
  getanswerformid,
  submitanswer,
  getanswer,
  sgetanswerformid
} from '@/api/answer'
import { getpaperfromid } from '@/api/paper'
// 答卷id
const id = ref(router.currentRoute._value.params.id)
id.value = parseInt(id.value)
const pid = ref(0)
// 答卷信息
const adata = ref([])
const pdata = ref([])
const showdata = ref([])
const iniadata = async () => {
  const res = await getanswerformid(id.value)
  adata.value = res.data.data.data
  //  获取答卷分页 对比id获取试卷id
  const queryForm = ref({
    pageIndex: 1,
    pageSize: 1000
  })
  const temp = await getanswer(queryForm.value)
  let paperlist = temp.data.data.list
  for (let i = 0; i < paperlist.length; i++) {
    if (paperlist[i].id === id.value) {
      pid.value = paperlist[i].examPaperId
    }
  }
  //  获取试卷文本
  const ptemp = await getpaperfromid(pid.value)
  pdata.value = ptemp.data.data.titleItems
  console.log(pdata.value)
  //  获取学生得分
  const stemp = await sgetanswerformid(id.value)
  console.log(adata.value)
  const xuanpdata = ref(stemp.data.data.paper.titleItems)
  //  展示信息汇总
  for (let i = 0; i < pdata.value.length; i++) {
    for (let j = 0; j < pdata.value[i].questionItems.length; j++) {
      if (adata.value[i].answerItems[j].answer === null) {
        adata.value[i].answerItems[j].answer =
          adata.value[i].answerItems[j].answerContent
      }
      showdata.value.push({
        correct: pdata.value[i].questionItems[j].correct,
        analyze: pdata.value[i].questionItems[j].analyze,
        title: pdata.value[i].questionItems[j].title,
        score: pdata.value[i].questionItems[j].score,
        questionItemObjects: xuanpdata.value[i].questionItems[j].items,
        questionId: adata.value[i].answerItems[j].questionId,
        answer: adata.value[i].answerItems[j].answer,
        endscore: null
      })
    }
  }
}
// 获取目前用户信息
console.log(showdata.value)
iniadata()
const submit = async () => {
  const subdata = ref({
    id: id.value, //  试卷id
    doTime: 1,
    uid: store.getters.id,
    answerItems: []
  })
  for (let i = 0; i < showdata.value.length; i++) {
    let temp = {
      content: showdata.value[i].endscore,
      completed: true,
      itemOrder: i + 1,
      questionId: showdata.value[i].questionId
    }
    subdata.value.answerItems.push(temp)
  }

  const test = await submitanswer(subdata.value)
  console.log('test', test)
  router.go(-1)
}
</script>

<style lang="scss" scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.box-card {
  width: 100%;
}
.el-button {
  float: inline-end;
}
</style>
